package com.java8feature.generic;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/5/12 15:33
 */
public class Java8FeatureTest {
    public static void main(String[] args) {

        TestT<B> t = new TestT<B>();
        System.out.println(t.currentModelClass());
    }
}

class A<T> {

}

class B<T> extends  A {

}
class TestT<T extends A> extends A{
    T t ;
    T getT() {
        return this.t;
    }

    protected Class<?> entityClass = this.currentModelClass();

    protected Class<?> currentModelClass() {
        return getSuperClassGenericType(this.getClass(), 1);
    }
    public static Class<?> getSuperClassGenericType(final Class<?> clazz, final int index) {
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            //logger.warn(String.format("Warn: %s's superclass not ParameterizedType", clazz.getSimpleName()));
            return Object.class;
        } else {
            Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
            if (index < params.length && index >= 0) {
                if (!(params[index] instanceof Class)) {
                   // logger.warn(String.format("Warn: %s not set the actual class on superclass generic parameter", clazz.getSimpleName()));
                    return Object.class;
                } else {
                    return (Class)params[index];
                }
            } else {
               // logger.warn(String.format("Warn: Index: %s, Size of %s's Parameterized Type: %s .", index, clazz.getSimpleName(), params.length));
                return Object.class;
            }
        }
    }


}